package com.aaa.ssm.controller;

import com.aaa.ssm.entity.Tuser;
import com.aaa.ssm.service.TuserService;
import com.aaa.ssm.util.BusinessException;
import com.aaa.ssm.util.Page;
import com.aaa.ssm.util.Result;
import com.aaa.ssm.util.SmsUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@RestController
@RequestMapping("tuser")
public class TuserController extends BaseController {
    @Resource
    private TuserService tuserService;

    private String verifyCode;

    @PostMapping("queryPage")
    public Result queryPage(@RequestBody Page<Tuser> page) {
        return success(tuserService.queryPage(page));
    }

    @PostMapping("add")
    public Result add(String mobile, String password ,String name, String smallname ,String sex) {
        Tuser tuser = new Tuser();
        tuser.setAccount(mobile);
        tuser.setMobile(mobile);
        tuser.setPassword(password);
        tuser.setName(name);
        tuser.setSmallname(smallname);
        tuser.setSex(sex);
        System.out.println("tuser = " + tuser);
        return success(tuserService.add(tuser));
    }

    /**
     * 注册时判断手机号是否存在
     * @param mobile
     * @return
     */
    @PostMapping("mobile")
    public Result queryMobile(Tuser mobile) {
        System.out.println("-------------------------------------------mobile = " + mobile);
        Tuser tuser = tuserService.queryMobile(mobile);
        if (tuser != null) {
            return error(500);
        }
        return success(200);
    }

    /**
     * 更新
     * @param tuser
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody Tuser tuser){
        return success(tuserService.update(tuser));
    }

    @PostMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        return success(tuserService.delete(id));
    }

    @PostMapping("login")
    public Result login(String loginName, String passWord ,HttpSession session) {
        Tuser tuser = new Tuser();
        tuser.setLoginName(loginName);
        tuser.setPassword(passWord);
        Tuser tuser1 = tuserService.checkUser(tuser);
        String name = tuser1.getName();
        int id = tuser1.getId();
        session.setAttribute("userA", id);
        session.setAttribute("nameA",name);
        if (tuser1 != null) {
            session.setAttribute("loginA", loginName);
            session.setAttribute("TUser", tuser1);
            return success(tuser1);
        } else {
            return error(tuser1);
        }
    }
    /**
     * 为前端传输登录的姓名
     * @param session
     * @return
     */
    @GetMapping("getLoginSession")
    public Result getLoginSession11(HttpSession session) {
        return success((String) session.getAttribute("nameA"));
    }
    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("logout")
    public void logout(HttpSession session) throws IOException {
        session.removeAttribute("loginA");
        session.removeAttribute("userA");
        session.removeAttribute("TUser");
        session.removeAttribute("nameA");
    }
    @PostMapping("createCode")
    public Result createCode(String mobile){
        verifyCode = SmsUtil.createVerifyCode();
        String s = SmsUtil.sendSms(mobile, verifyCode);
        return success(s);
    }
    @PostMapping("yanzheng")
    public Result yanzheng(String numcode){
        System.out.println("输入验证码---------------------"+numcode);
        System.out.println("验证码-------------------------------------------"+verifyCode);
        if (numcode == verifyCode){
            return success(200);
        }else {
            return error(500);
        }
    }


    /**
     * 订单判断信息
     * @param session
     * @return
     */
    @GetMapping("tick")
    public Result tick(HttpSession session){
        Tuser checkUser = (Tuser)session.getAttribute("TUser");
        Result success = success(checkUser);
        if (success.getData()==null){
            throw new BusinessException("请登录后下单");
        }
        return success;
    }
}
